home *** CD-ROM | disk | FTP | other *** search
/ SGI Performance Co-Pilot 1.3 / SGI Performance Co-Pilot 1.3.iso / dist / pcp.idb / usr / pcp / bin / cron.pmdaily.z / cron.pmdaily
Text File  |  1997-04-03  |  7KB  |  305 lines

  1. #!/bin/sh
  2. #
  3. # Copyright 1995, Silicon Graphics, Inc.
  4. # ALL RIGHTS RESERVED
  5. # UNPUBLISHED -- Rights reserved under the copyright laws of the United
  6. # States.   Use of a copyright notice is precautionary only and does not
  7. # imply publication or disclosure.
  8. # U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  9. # Use, duplication or disclosure by the Government is subject to restrictions
  10. # as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  11. # in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  12. # in similar or successor clauses in the FAR, or the DOD or NASA FAR
  13. # Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  14. # 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  15. # THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  16. # INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  17. # DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  18. # PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  19. # GRAPHICS, INC.
  20. #
  21. # Example daily administrative script for PCP archive logs
  22. #
  23.  
  24. # constant setup
  25. #
  26. tmp=/tmp/$$
  27. status=0
  28. trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
  29. prog=`basename $0`
  30.  
  31. # control file for pmlogger administration ... edit the entries in this
  32. # file to reflect your local configuration (see also -c option below)
  33. #
  34. CONTROL=/var/pcp/config/pmlogger/control
  35.  
  36. # default number of days to keep archive logs
  37. #
  38. CULLAFTER=14
  39.  
  40. # threshold size to roll /var/adm/pcplog/NOTICES
  41. #
  42. NOTICES=/var/adm/pcplog/NOTICES
  43. ROLLNOTICES=20480
  44.  
  45. # determine real name for localhost
  46. LOCALHOST=""
  47. [ -x /usr/bsd/hostname ] && LOCALHOST=`/usr/bsd/hostname`
  48.  
  49. # option parsing
  50. #
  51. showme=false
  52. usage="Usage: $prog [-c control] [-s size] [-k howlong]"
  53. while getopts c:k:ns:? c
  54. do
  55.     case $c
  56.     in
  57.     c)    CONTROL="$OPTARG"
  58.         ;;
  59.     k)    CULLAFTER="$OPTARG"
  60.         check=`echo "$CULLAFTER" | sed -e 's/[0-9]//g'`
  61.         if [ "X$check" != "X" -a "X$check" != "Xforever" ]
  62.         then
  63.             echo "Error: -k option ($CULLAFTER) must be numeric"
  64.             status=1
  65.             exit
  66.         fi
  67.         ;;
  68.     n)    showme=true
  69.         ;;
  70.     s)    ROLLNOTICES="$OPTARG"
  71.         check=`echo "$ROLLNOTICES" | sed -e 's/[0-9]//g'`
  72.         if [ "X$check" != "X" ]
  73.         then
  74.             echo "Error: -s option ($ROLLNOTICES) must be numeric"
  75.             status=1
  76.             exit
  77.         fi
  78.         ;;
  79.     ?)    echo "$usage"
  80.         status=1
  81.         exit
  82.         ;;
  83.     esac
  84. done
  85. shift `expr $OPTIND - 1`
  86.  
  87. if [ $# -ne 0 ]
  88. then
  89.     echo "$usage"
  90.     status=1
  91.     exit
  92. fi
  93.  
  94. if [ ! -f $CONTROL ]
  95. then
  96.     echo "$prog: Error: cannot find control file ($CONTROL)"
  97.     status=1
  98.     exit
  99. fi
  100.  
  101. # each new archive log started by pmnewlog is named yymmdd.hh.mm
  102. #
  103. LOGNAME=`date "+%y%m%d.%H.%M"`
  104.  
  105. # each summarized log is named yymmdd
  106. #
  107. PREV_LOGNAME=`pmdate -1d %y%m%d`
  108.  
  109. _error()
  110. {
  111.     _report Error "$1"
  112. }
  113.  
  114. _warning()
  115. {
  116.     _report Warning "$1"
  117. }
  118.  
  119. _report()
  120. {
  121.     echo "$prog: $1: $2"
  122.     echo "[$CONTROL:$line] ... logging for host \"$host\" unchanged"
  123.     touch $tmp.err
  124.     continue
  125. }
  126.  
  127. # Roll /var/adm/pcplog/NOTICES -> /var/adm/pcplog/NOTICES.old if larger
  128. # that 10 Kbytes
  129. #
  130. NOTICES=/var/adm/pcplog/NOTICES
  131. if [ -s $NOTICES ]
  132. then
  133.     if [ "`wc -c <$NOTICES`" -ge $ROLLNOTICES ]
  134.     then
  135.     echo "Roll $NOTICES -> $NOTICES.old"
  136.     echo "Start new $NOTICES"
  137.     if $showme
  138.     then
  139.         :
  140.     else
  141.         echo >>$NOTICES
  142.         echo "*** rotated by $prog: `date`" >>$NOTICES
  143.         mv -f $NOTICES $NOTICES.old
  144.         echo "Started by $prog: `date`" >$NOTICES
  145.     fi
  146.     fi
  147. fi
  148.  
  149. # note on control file format version
  150. #  1.0 was shipped as part of PCPWEB beta, and did not include the
  151. #    socks field [this is the default for backwards compatibility]
  152. #  1.1 is the first production release, and the version is set in
  153. #    the control file with a $version=1.1 line (see below)
  154. #
  155.  
  156. rm -f $tmp.err
  157. line=0
  158. version=1.0
  159. cat $CONTROL \
  160. | sed -e "s/LOCALHOSTNAME/$LOCALHOST/g" \
  161. | while read host primary socks dir args
  162. do
  163.     line=`expr $line + 1`
  164.     case "$host"
  165.     in
  166.     \#*|'')    # comment or empty
  167.         continue
  168.         ;;
  169.     \$*)    # in-line shell command
  170.         cmd=`echo "$host $primary $socks $dir $args" | sed -e 's/^\\$//'`
  171.         eval $cmd
  172.         continue
  173.         ;;
  174.     esac
  175.  
  176.     if [ "$version" = "1.0" ]
  177.     then
  178.     args="$dir $args"
  179.     dir="$socks"
  180.     socks=n
  181.     fi
  182.  
  183.     if [ ! -d $dir ]
  184.     then
  185.     _error "archive directory ($dir) does not exist"
  186.     continue
  187.     fi
  188.     cd $dir
  189.     echo
  190.     echo "=== daily maintenance of PCP archives for host $host ==="
  191.     echo
  192.  
  193.     if [ "X$primary" = Xy ]
  194.     then
  195.     if [ "X$host" != "X$LOCALHOST" ]
  196.     then
  197.         _error "\"primary\" only allowed for $LOCALHOST (localhost, not $host)"
  198.         continue
  199.     fi
  200.     if /sbin/chkconfig pmlogger
  201.     then
  202.         :
  203.     else
  204.         _warning "primary logging disabled via chkconfig for $host"
  205.         continue
  206.     fi
  207.  
  208.     pid=`ps -ef \
  209.          | sed -n \
  210.          -e '/grep/d' \
  211.          -e 's/$/ /' \
  212.          -e '/\/usr\/pcp\/bin\/pmlogger .*-P /p' \
  213.          | nawk '{print $2}'`
  214.     else
  215.     pid=`ps -ef \
  216.          | sed -n \
  217.          -e '/grep/d' \
  218.          -e 's/$/ /' \
  219.          -e "/\/usr\/pcp\/bin\/pmlogger .*-h *$host /p" \
  220.          | nawk '{print $2}'`
  221.     fi
  222.  
  223.     if [ "X$pid" = X ]
  224.     then
  225.     _error "no pmlogger instance running for host \"$host\""
  226.     else
  227.     if [ "`echo $pid | wc -w`" -gt 1 ]
  228.     then
  229.         _error "multiple pmlogger instances running for host \"$host\", pids: $pid"
  230.         continue
  231.     fi
  232.  
  233.     # now execute pmnewlog to "roll the archive logs"
  234.     #
  235.     [ "X$primary" != Xy ] && args="-p $pid $args"
  236.     [ "X$socks" = Xy ] && args="-s $args"
  237.     if $showme
  238.     then
  239.         echo "+ /usr/pcp/bin/pmnewlog $args $LOGNAME"
  240.     else
  241.         if /usr/pcp/bin/pmnewlog $args $LOGNAME
  242.         then
  243.         :
  244.         else
  245.         _error "problems executing pmnewlog for host \"$host\""
  246.         touch $tmp.err
  247.         fi
  248.     fi
  249.     fi
  250.  
  251.     # concatenate yesterday's archive logs
  252.     #
  253.     echo
  254.     if [ -f $PREV_LOGNAME.0 -o -f $PREV_LOGNAME.index -o -f $PREV_LOGNAME.meta ]
  255.     then
  256.     echo "$prog: Warning: output archive ($PREV_LOGNAME) already exists"
  257.     echo "[$CONTROL:$line] ... skip log merging for host \"$host\""
  258.     continue
  259.     fi
  260.     if $showme
  261.     then
  262.     echo "+ /usr/pcp/bin/cron.pmlogmerge -f $PREV_LOGNAME'.??.??' $PREV_LOGNAME"
  263.     else
  264.     if /usr/pcp/bin/cron.pmlogmerge -f $PREV_LOGNAME'.??.??' $PREV_LOGNAME
  265.     then
  266.         :
  267.     else
  268.         _error "problems executing cron.pmlogmerge for host \"$host\""
  269.     fi
  270.     fi
  271.  
  272.     # and cull old archives
  273.     #
  274.     if [ "X$CULLAFTER" != "Xforever" ]
  275.     then
  276.     echo "/^[0-9][0-9][0-1][0-9][0-3][0-9]\./!d" >$tmp.sed
  277.     i=0
  278.     while [ $i -le $CULLAFTER ]
  279.     do
  280.         echo "/^`pmdate -${i}d %y%m%d`\./d" >>$tmp.sed
  281.         i=`expr $i + 1`
  282.     done
  283.  
  284.     LIST=`ls | sed -f $tmp.sed`
  285.     if [ "X$LIST" != X ]
  286.     then
  287.         echo "Archive files older than $CULLAFTER days being removed ..."
  288.         echo "$LIST" | fmt | sed -e 's/^/    /'
  289.         if $showme
  290.         then
  291.         :
  292.         else
  293.         echo "$LIST" | xargs rm -f
  294.         fi
  295.     fi
  296.     fi
  297.  
  298. done
  299.  
  300. [ -f $tmp.err ] && status=1
  301. exit
  302.